import api from "@/api/modules/user";
import store from "@/store";
const state = {
  TOKEN: null,
  isLogin: false,
  userInfo: null,
  systemAvatars: [], // 系统头像列表
  taskReceiveingNum: 0, // 任务等待领取的数量
  messageNotReadNum: 0, // 未读消息数量
};

const actions = {
  // 验证码登录 {mobile,code}
  fastLogin(context, formData) {
    return api.fastLogin(formData).then(response => {
      context.commit("SET_TOKEN", response.data.Authorization);
      // 设置等待领取的任务数量
      store.commit("socket/task_receiveing", {
        task: response.data.task.num,
        invite: 0
      });
      return response.data;
    });
  },
  // 密码登录{mobile,password}
  login(context, formData) {
    return api.login(formData).then(response => {
      context.commit("SET_TOKEN", response.data.Authorization);
      // 设置等待领取的任务数量
      store.commit("socket/task_receiveing", {
        task: response.data.task.num,
        invite: 0
      });
      return response.data;
    });
  },
  // 用户名注册登录
  unameLogin(context, formData) {
    return api.unameLogin(formData).then(response => {
      context.commit("SET_TOKEN", response.data.Authorization);
      // 设置等待领取的任务数量
      store.commit("socket/task_receiveing", {
        task: response.data.task.num,
        invite: 0
      });
      return response.data;
    });
  },
  oauthLogin(context, data = {
    type,
    userinfo
  }) {
    return api.oauthLogin(data.type, data.userinfo).then(response => {
      context.commit("SET_TOKEN", response.data.Authorization);
      // 设置等待领取的任务数量
      store.commit("socket/task_receiveing", {
        task: response.data.task.num,
        invite: 0
      });
      return response.data;
    });
  },
  getUserInfo(context) {
    return api.getUserInfo().then(response => {
      context.commit("SET_USER_INFO", response.data.data);
      return response;
    });
  },
  // 获取系统头像
  getSystemAvatars(context) {
    return api.getSystemAvatars().then(_ => {
      context.commit("SET_SYSTEM_AVATARS", _.data.data);
      return _.data.data;
    });
  },
  // 修改密码
  changePassword(context, data = {
    oldPassword,
    newPassword
  }) {
    return api.changePassword(data.oldPassword, data.newPassword).then(response => {
      let userinfo = store.state.user.userInfo;
      userinfo.hasPassword = 1;
      context.commit("SET_USER_INFO", userinfo);
      return response;
    });
  },
  // 设置头像
  setAvatar(context, formData = {
    avatar
  }) {
    return api.setAvatar(formData).then(response => {
      let userinfo = store.state.user.userInfo;
      userinfo.avatar = formData.avatar;
      context.commit("SET_USER_INFO", userinfo);
      return response;
    });
  },
  // 修改昵称
  setNickName(context, formData = {
    nickName
  }) {
    return api.setNickName(formData).then(response => {
      let userinfo = store.state.user.userInfo;
      userinfo.nickName = formData.nickName;
      context.commit("SET_USER_INFO", userinfo);
      return response;
    });
  },
  // 设置性别
  setSex(context, formData = {
    sex
  }) {
    return api.setSex(formData).then(response => {
      let userinfo = store.state.user.userInfo;
      userinfo.sex = formData.sex || 0;
      context.commit("SET_USER_INFO", userinfo);
      return response;
    });
  },
  // 设置个性签名
  setSignature(context, formData = {
    signature
  }) {
    return api.setSignature(formData).then(response => {
      let userinfo = store.state.user.userInfo;
      userinfo.signature = formData.signature || "";
      context.commit("SET_USER_INFO", userinfo);
      return response;
    });
  },
  // 退出登录
  logout(context) {
    return api.logout().then(_ => {
      context.commit("LOGOUT");
      return _;
    });
  },
  // 销户
  logoff(context) {
    return api.logoff().then(_ => {
      context.commit("LOGOUT");
      return _;
    });
  }
};

const mutations = {
  // 设置token
  SET_TOKEN(state, token) {
    state.isLogin = true;
    state.TOKEN = token;
    // 获取用户信息
    store.dispatch("user/getUserInfo");
    getApp().globalData.socket.config.params = {
      token: token
    };
    getApp().globalData.socket.sendSocketMessage({
      c: "user",
      a: "register",
      token: state.TOKEN
    });
  },
  // 删除token
  DELETE_TOKEN(state) {
    state.TOKEN = null;
    state.isLogin = false;
  },
  SET_USER_INFO(state, data) {
    state.userInfo = data;
    state.isLogin = true;
  },
  SET_SYSTEM_AVATARS(state, data) {
    state.systemAvatars = data;
  },
  // 退出登录
  LOGOUT(state) {
    state.isLogin = false;
    state.TOKEN = null;
    uni.clearStorage();
    uni.clearStorageSync();
  }
};


export default {
  namespaced: true,
  state,
  actions,
  mutations
};
